package bkq.viettravel.bll;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.HttpConnection;

import android.content.Context;
import android.net.ConnectivityManager;
import android.util.Log;



public class ConnectInternet {


	/**
	 * - Author:Dao Viet Danh 
	 * - Describe:Check connected Internet for devices
	 * - Date: 09/07/2011 
	 * @param context
	 * Context for Activity
	 * @return
	 * True if Success else false 
	 */
	public static  boolean checkConnectInternet(Context context) {
		
		ConnectivityManager conMa=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		try{	
		if (conMa.getActiveNetworkInfo() != null

			&& conMa.getActiveNetworkInfo().isAvailable()

		    && conMa.getActiveNetworkInfo().isConnected()) {

				return true;
				
			}
		else return false;
		}
		catch(Exception ex){
			Log.d("Test",ex.getMessage());
			return false;
		}
		
	}
	
	
	/**
	 * Author: Dao Viet Danh 
	 * Describe : get InputStream from url
	 * Date : 09/09/2011
	 * @param address
	 * url which your input 
	 * @return
	 * InputStream 
	 * @throws Exception
	 */
	 public static InputStream  openHttpConnect(String address) throws Exception{

		 InputStream in = null;
		 int checkCode = -1;
		 
		 		 
		 URL url=new URL(address);
		 URLConnection urlcon=url.openConnection();
		 
		 if(!(urlcon instanceof HttpURLConnection)){
			 throw new Exception("Not must httpConnect");
		 }
		 
		 try{
			 
			 HttpURLConnection httpurl=(HttpURLConnection)urlcon;
			 
			 httpurl.setAllowUserInteraction(true);
			 httpurl.setInstanceFollowRedirects(true);
			 httpurl.setRequestMethod("GET");
			 httpurl.connect();
			 checkCode = httpurl.getResponseCode();
			 
			 if(checkCode == HttpURLConnection.HTTP_OK){
				 
				 in = httpurl.getInputStream();
			 }			 
			 
		 }
		 catch(Exception ex){
			 
			 throw new Exception("Get not implement");
		 }
		 return in ;
		 
		 
		 
		 
		 
		 
	 }
	 
}
 
